package com.xingtu.testSerialport;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.app.ActivityCompat;
import androidx.multidex.MultiDex;

import com.bumptech.glide.Glide;
import com.xingtu.testSerialport.dialog.LoadingDialog;
import com.xingtu.testSerialport.receiver.NetworkStateReceiver;
import com.zcx.helper.app.AppApplication;
import com.zcx.helper.app.AppInit;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.Http;

/**
 * Author: 雅典娜
 * Created on 2024/03/01
 * 别总是抱怨生活不够幸运，是你欠了生活一份努力，
 * 每一个你讨厌的现在，都有一个不够努力的曾经，
 * 未来美不美，取决于你现在拼不拼。
 * Desc:MyApplication
 */
@AppInit(name = "serialportDemp", log = true, crash = true)
public class MyApplication extends AppApplication {


    public static String FILE_PATH = "serialportDemp/image/";
    public static MyPreferences myPreferences;

    public static Context mAppContext;
    public static LoadingDialog loadingDialog;

    public NetworkStateReceiver getNetworkReceiver() {
        return networkReceiver;
    }

    public NetworkStateReceiver networkReceiver;
    public static MyApplication sInstance;


    // 实例化一次
    public synchronized static MyApplication getInstance() {
        return sInstance;
    }


    @Override
    public void onCreate() {
        MultiDex.install(getApplicationContext());
        super.onCreate();
        sInstance = this;
        mAppContext = this;
        registerActivityLifecycleCallbacks(lifecycleCallbacks);
        try {
            myPreferences = new MyPreferences(this);

            // 注册BroadCastReciver,设置监听的频道。就是filter中的
            networkReceiver = new NetworkStateReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
            filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
            filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            registerReceiver(networkReceiver, filter);
            //TODO 初始化高德地图
            initSDK(this);

            GlideLoader.getInstance().init(R.color.background_color, R.color.background_color);

            Http.getInstance().allTimeout(60);

            Http.getInstance().setOnStartEndCreateDialog(new Http.OnStartEndCreateDialog<LoadingDialog>() {

                @Override
                public void show(LoadingDialog dialog) {
                    loadingDialog = dialog;
                    dialog.show();

                }

                @Override
                public void dismiss(LoadingDialog dialog) {

                    dialog.dismiss();

                }

                @Override
                public LoadingDialog create(Context context) throws Exception {

                    return new LoadingDialog(context);

                }

            });

            AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);//启用矢量图兼容


        } catch (Exception e) {

        }
    }

    public static void initSDK(Context context) {
    }
    /**
     * 获取设备唯一ID
     *
     * @param context
     * @return
     */
    @SuppressLint("MissingPermission")
    public static String getDeviceUniqID(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (ActivityCompat.checkSelfPermission(MyApplication.getInstance().getApplicationContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                return "";
            }
            try {
                return Build.getSerial();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return Build.SERIAL;
        //return "3100S08392300743";
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }


    public static Context getContext() {
        return mAppContext;
    }


    //监听是否有网络 没有网络的时候显示 无网络提示
    public boolean isNetworkAvailable() {

        NetworkInfo networkInfo = getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isAvailable();
    }

    public NetworkInfo getActiveNetworkInfo() {
        return ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    }


    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        if (level == TRIM_MEMORY_UI_HIDDEN) {

            Glide.get(this).clearMemory();
        }

        Glide.get(this).trimMemory(level);
    }


    private Application.ActivityLifecycleCallbacks lifecycleCallbacks = new Application.ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {

        }

        @Override
        public void onActivityStarted(@NonNull Activity activity) {

        }

        @Override
        public void onActivityResumed(@NonNull Activity activity) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                }
            }, 1000);


        }

        @Override
        public void onActivityPaused(@NonNull Activity activity) {

        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {

        }
    };

    @Override
    public void onTerminate() {
        unregisterActivityLifecycleCallbacks(lifecycleCallbacks);
        super.onTerminate();
    }
}
